//
//  TKBaseViewController.swift
//  TKWBOne
//
//  Created by tiankun on 16/8/22.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit

class TKBaseViewController: UIViewController {
    
    //访客视图信息字典
    var visitorInfoDict: [String:String]?
    //表格视图
    var tableView: UITableView?
    //刷新控件
    var refreshControl: UIRefreshControl?
    //上拉刷新标记
    var isPullup = false
    //自定义导航条
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    //自定义导航条目
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupUI()
        TKNetworkManager.shared.userLogon ? loadData() : ()
        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess(n:)), name: NSNotification.Name(rawValue: TKUserLoginSuccessedNotification), object: nil)
    }
    deinit {
        //注销通知
        NotificationCenter.default.removeObserver(self)
    }
    override var title: String? {
        didSet{
            navItem.title = title
        }
    }
    func loadData() {
        refreshControl?.endRefreshing()
    }
    

}
//访客视图监听方法
extension TKBaseViewController {
    @objc private func register() {
        print("注册")
    }
    @objc private func login() {
        //发送通知
        NotificationCenter.default.post(name: NSNotification.Name(TKUserShouldLoginNotification), object: nil)
    }
    //登陆成功通知方法
    @objc private func loginSuccess(n: Notification) {
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        view = nil
        NotificationCenter.default.removeObserver(self)
    }
}

extension TKBaseViewController {
    
    private func setupUI() {
        view.backgroundColor = UIColor.white
        automaticallyAdjustsScrollViewInsets = false
        setupNavigationBar()
        TKNetworkManager.shared.userLogon ? setupTableView() : setupVisitorView()
    }
    private func setupNavigationBar() {
        view.addSubview(navigationBar)
        navigationBar.items = [navItem]
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.darkGray]
        navigationBar.tintColor = UIColor.orange
    }
    
    private func setupVisitorView() {
        let visitorView = TKVisitorView(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: navigationBar)
        visitorView.infoDictionary = visitorInfoDict
        visitorView.registerBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        visitorView.loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))
        
        
    }
    func setupTableView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        view.insertSubview(tableView!, belowSubview: navigationBar)
        tableView?.dataSource = self
        tableView?.delegate = self
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        loadData()
    }

}
extension TKBaseViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        if row < 0 || section < 0 {
            return
        }
        let count = tableView.numberOfRows(inSection: section)
        if row == (count - 1) && !isPullup {
            print("上拉刷新")
            isPullup = true
            loadData()
        }
    }

}
